makeMultiIndex
creates an expanded set of indices to referencce a
regular grid. M are L integers with product prodM
Will create a prodM by L matrix that is all combinations of (1:M[i]) for i =1,2, ...L
This is organized in the standard array ordering where the first
column varies the fastest for M = c( 3,2,4) the result will be a 24X3 matrix
with the entries:
1,1,1
2,1,1
3,1,1
1,2,1
2,2,1
3,2,1
etc ...
...
and ending with
2,2,4
3,2,4
All about grid lists:
The form of a grid.list is
list( var.name1= what1 , var.name2=what2 , ... var.nameN=what3)
Here var.names are the names of the independent variables.
The what options describe what should be done with this variable when
generating the grid. These should either an increasing sequence of points
or a single vaules. Obviously there should be only be two variables with
sequences to define a grid for a surface.
Most of time the gridding sequences are equally
spaced and are easily generated using the seq
function. Also throughout fields
the grid points are typically the midpoints of the grid rather the grid box
boundaries. However, these functions can handle unequally spaced grids and the
logical boundary.grid can indicate a grid being the box boundaries.
The variables in the list components are assumed
to be in the same order as they appear in the data matrix.
A useful function that expands the grid from the grid.list description into
a full set of locations is make.surface.grid
and is
just a wrapper around the R base function expand.grid
. A typical operation is to go from a grid.list to the set of grid locations. Evaluate a
fucntion at these lcoations and then reformat this as an image for plotting.
Here is how to do this cleanly:
grid.list<- list( x= 1:10, y=1:15)
xg<- make.surface.grid(grid.list)
# look at a surface dependin on xg locations
z<- xg[,1] + 2*xg[,2]
out<- list( x=grid.list$x, y= grid.list$y, z=matrix( z, nrow=10, ncol=15))
# now for example
image.plot( out)
The key here is that xg
and matrix
both organize the grid in the
same order.
Some fields internal functions that support interpreting grid list format are:
fields.x.to.grid
:
Takes an "x" matrix of locations or independent variables and creates a
reasonable grid list. This is used to evaluate predicted surfaces when a
grid list is not explicited given to predictSurface. The variables
(i.e. columns of x) that are not part of the grid are set to the median
values. The x grid values are nx
equally spaced points in the
range x[, xy[1]]
. The y grid values are ny
equally spaced
points in the range x[, xy[2]]
.
parse.grid.list
:
Takes a grid list and returns the information in a more expanded list
form that is easy to use. This is used, for example, by predictSurface
to figure out what to do!
fields.convert.grid
:
Takes a vector of n values assumed to be midpoints of a grid and
returns the n+1 boundaries. See how this is used in discretize.image
with the cut function. This function will handle unequally spaced
grid values.
discretize.image
: Takes a vector of locations and a 2-d grid and
figures out to which boxes they belong. The output matrix ind has the
grid locations. If boundary.grid is FALSE then the grid list (grid) is
assumed to be grid midpoints. The grid boundaries are taken to be the
point half way between these midpoints. The first and last boundaries
points are determined by extrapolating so that the first and last box
has the midpoint in its center. (See the code in fields.convert.grid for
details.) If grid is NULL then midpoints are found from m and n and the
range of the x matrix.
unrollZGrid
Checks that the ZGrid object is compatible with th e grid.list and concatenates the grid arrays into vectors. This version of the covariates are used the usual predict function.